#include "mainwindow.h"
#include "loginwindow.h"
#include "ui_mainwindow.h"
#include "infowindow.h"
#include "changepasswordwindow.h"
#include "tableviewdelegate.h"
#include <QDebug>
#include <QFont>
#include <QColor>
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include <QPushButton>
#include <QCalendarWidget>
#include <QtSql/QSqlQueryModel>
#include <QtSql/QSqlQuery>
#include <QSqlError>
#include <QFileDialog>
#include <QtCharts/QtCharts>
#include <QtCharts/QChartView>
#include <QSizePolicy>
#include <iomanip>
#include <iostream>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QAbstractItemModel>
#include <QItemSelectionModel>
#include <QModelIndexList>
#include <QModelIndex>
#include <QMap>
#include <QMapIterator>
#include <QMediaPlayer>
#include <QPixmap>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("心灵账本");
    addWindow = new addItem();
    //设置字体
    int fontId = QFontDatabase::addApplicationFont(":/imageUsed/character.ttf");
    QStringList fontIDs = QFontDatabase::applicationFontFamilies(fontId);
    QFont font(fontIDs.first());
    QApplication::setFont(font);

    setYearMonthBox();

    //设置主界面字体大小
    QFont ft;
    ft.setPointSize(12);
    ui->label_3->setFont(ft);
    ui->label_4->setFont(ft);
    ui->label_5->setFont(ft);
    ui->label_6->setFont(ft);
    ui->cur_month_day->setFont(ft);
    ft.setPointSize(20);
    ft.setBold(true);
    ui->label->setFont(ft);
    ui->label_2->setFont(ft);

    //设置默认定位
    ui->stackedWidget->setCurrentIndex(1);
    ui->chartView->setCurrentIndex(0);

    //按年按月切换
    connect(ui->radioButton_4,SIGNAL(toggled(bool)),
            this, SLOT(radioButtonFun_4()));

    //选择的年份月份改变
    connect(ui->yearBox, &QComboBox::currentTextChanged,[=](){
        int yearChoose=ui->yearBox->currentText().toInt();
        int monthChoose=(ui->monthBox->currentIndex())+1;
        if(ui->radioButton_5->isChecked())
            ui->chartView->setCurrentWidget(chartPieWidget(yearChoose,monthChoose));
        else
            ui->chartView->setCurrentWidget(chartPieWidget(yearChoose,monthChoose));
    });
    connect(ui->monthBox, &QComboBox::currentTextChanged,[=](){
        int yearChoose=ui->yearBox->currentText().toInt();
        int monthChoose=(ui->monthBox->currentIndex())+1;
        if(ui->radioButton_5->isChecked())
            ui->chartView->setCurrentWidget(chartPieWidget(yearChoose,monthChoose));
        else
            ui->chartView->setCurrentWidget(chartPieWidget(yearChoose,monthChoose));
    });

    //关闭记账窗口时刷新账单并判断是否超额
    connect(addWindow,&addItem::windowClose,[=](){
        mainCashBook();
        setLabel();
        judgeBudget();
    });

    //取消按钮边框
    ui->music_btn->setStyleSheet("QPushButton{color:white; background-color:transparent;}");
    ui->theme_btn->setStyleSheet("QPushButton{color:white; background-color:transparent;}");
}

//更换主题
void MainWindow::changeTheme()
{
    ui->barSide->setAutoFillBackground(true); // 这句要加上, 否则可能显示不出背景图.
    QPalette palLeft = ui->barSide->palette();
    palLeft.setBrush(QPalette::Window,
                     QBrush(QPixmap(":/imageUsed/background_dark.png").scaled( ui->barSide->size(),
                                                                               Qt::IgnoreAspectRatio,
                                                                               Qt::SmoothTransformation))); // 使用平滑的缩放方式
    ui->barSide->setPalette(palLeft);    // 已给widget加上了背景图.

    QPixmap mainMap(":/imageUsed/user_blue.png");
    mainMap.scaled(ui->main_btn->width(),ui->main_btn->height());
    ui->main_btn->setIcon(mainMap);
    ui->main_btn->setFixedSize(ui->main_btn->size());

    QPixmap check(":/imageUsed/check_white.png");
    check.scaled(ui->check_btn->width(),ui->check_btn->height());
    ui->check_btn->setIcon(check);
    ui->check_btn->setFixedSize(ui->check_btn->size());

    QPixmap chart(":/imageUsed/chart_white.png");
    chart.scaled(ui->chart_btn->width(),ui->chart_btn->height());
    ui->chart_btn->setIcon(chart);
    ui->chart_btn->setFixedSize(ui->chart_btn->size());

    QPixmap info(":/imageUsed/setting_white.png");
    info.scaled(ui->info_btn->width(),ui->info_btn->height());
    ui->info_btn->setIcon(info);
    ui->info_btn->setFixedSize(ui->info_btn->size());

    if(ui->theme_btn->isChecked())       //夜间模式
    {
        //更改字体颜色
        ui->mainWindow->setStyleSheet("QLabel{color:white;}");
        ui->chartWindow->setStyleSheet("QLabel{color:white;}");
        ui->setWindow->setStyleSheet("QLabel{color:white;}");
        //日历
        {
            ui->calendarWidget->setStyleSheet("QCalendarWidget QTableView{"
                                              "alternate-background-color: rgb(11,60,92);"
                                              "background-color: rgb(11,60,92);"
                                              "color: white;"
                                              "Border: 1px solid white;}"
                                              "QToolButton#qt_calendar_monthbutton,#qt_calendar_yearbutton{"
                                              "color: #3A3A3A;}"
                                              "QCalendarWidget QWidget#qt_calendar_navigationbar{"
                                              "background-color: white;}");
        }

        //checkWindow按钮颜色
        ui->checkWindow->setStyleSheet("QPushButton{"
                                       "border:2px solid rgb(255, 255, 255);"
                                       "background: transparent;"
                                       "border-radius: 4px;"
                                       "color:white;}"
                                       "QPushButton:hover{"
                                       "background-color: rgb(89,99,99);}");

        //limitWindow按钮颜色
        ui->limitWindow->setStyleSheet("QPushButton{"
                                       "border:none;"
                                       "background: transparent;"
                                       "color:white;}"
                                       "QLabel{color:white}");


        //返回主界面
        QPixmap back(":/imageUsed/back_white.png");
        back.scaled(ui->pushButton->width(),ui->pushButton->height());
        ui->pushButton->setIcon(back);
        ui->pushButton->setFixedSize(ui->pushButton->size());

        //年份月份样式表
        ui->yearBox->setStyleSheet("QComboBox{"
                                   "border:2px solid rgb(255, 255, 255);"
                                   "background: transparent;"
                                   "border-radius: 4px;"
                                   "color:white;}"
                                   "QComboBox QAbstractItemView {background:rgb(5,94,134);"
                                   "selection-background-color: rgb(89,99,99);"
                                   "color:white;}");
        ui->monthBox->setStyleSheet("QComboBox{"
                                    "border:2px solid rgb(255, 255, 255);"
                                    "background: transparent;"
                                    "border-radius: 4px;"
                                    "color:white;}"
                                    "QComboBox QAbstractItemView {background:rgb(5,94,134);"
                                    "selection-background-color:rgb(89,99,99);"
                                    "color:white;}");

        //radioButton样式表
        ui->radioButton->setStyleSheet("QRadioButton{color:white;}");
        ui->radioButton_2->setStyleSheet("QRadioButton{color:white;}");
        ui->radioButton_3->setStyleSheet("QRadioButton{color:white;}");
        ui->radioButton_4->setStyleSheet("QRadioButton{color:white;}");
        ui->radioButton_5->setStyleSheet("QRadioButton{color:white;}");

        //tableview样式表
        ui->showTableView->setStyleSheet("QTableView{"
                                         //"Selection-background-color:"//选中条目的背景色
                                         "Background-color:transparent;"//整个表格的背景色
                                         "Border：1px solid rgb(255,255,255);"
                                         "color:white;}"
                                         );
        ui->editTableView->setStyleSheet("QTableView{"
                                         //"Selection-background-color:"//选中条目的背景色
                                         "Background-color:transparent;"//整个表格的背景色
                                         "Border：1px solid rgb(255,255,255);"
                                         "color:white;}"
                                         );

        //箭头
        ui->arrow->setPixmap(QPixmap(":/imageUsed/arrow_white.png"));
        ui->arrow->setScaledContents(true);

        //温馨label
        ui->label_13->setStyleSheet("QLabel{color:rgb(255,255,255);}");

        //深色背景
        ui->stackedWidget->setAutoFillBackground(true);
        QPalette palDark = ui->stackedWidget->palette();
        palDark.setBrush(QPalette::Window,
                         QBrush(QPixmap(":/imageUsed/background_dblue.png").scaled( ui->stackedWidget->size(),
                                                                                    Qt::IgnoreAspectRatio,
                                                                                    Qt::SmoothTransformation)));
        ui->stackedWidget->setPalette(palDark);

        //添加按钮
        QPixmap addWhite(":/imageUsed/add_white.png");
        addWhite.scaled(ui->add_btn->width(),ui->add_btn->height());
        ui->add_btn->setIcon(addWhite);
        ui->add_btn->setFixedSize(ui->add_btn->size());

        //添加限额
        QPixmap setLimitWhite(":/imageUsed/featherPen_white.png");
        setLimitWhite.scaled(ui->limit_btn->width(),ui->limit_btn->height());
        ui->limit_btn->setIcon(setLimitWhite);
        ui->limit_btn->setFixedSize(ui->limit_btn->size());

        //图表三个按钮
        QPixmap pieWhite(":/imageUsed/pie_white.png");
        pieWhite.scaled(ui->pie_btn->width(),ui->pie_btn->height());
        ui->pie_btn->setIcon(pieWhite);
        ui->pie_btn->setFixedSize(ui->pie_btn->size());

        QPixmap barWhite(":/imageUsed/bar_white.png");
        barWhite.scaled(ui->bar_btn->width(),ui->bar_btn->height());
        ui->bar_btn->setIcon(barWhite);
        ui->bar_btn->setFixedSize(ui->bar_btn->size());

        QPixmap lineWhite(":/imageUsed/line_white.png");
        lineWhite.scaled(ui->line_btn->width(),ui->line_btn->height());
        ui->line_btn->setIcon(lineWhite);
        ui->line_btn->setFixedSize(ui->line_btn->size());

        //音效
        if(ui->music_btn->isChecked())
        {
            QPixmap music(":/imageUsed/nomusic_white.png");
            music.scaled(ui->music_btn->width(),ui->music_btn->height());
            ui->music_btn->setIcon(music);
            ui->music_btn->setFixedSize(ui->music_btn->size());
        }
        else if(!ui->music_btn->isChecked())
        {
            QPixmap nomusic(":/imageUsed/music_white.png");
            nomusic.scaled(ui->music_btn->width(),ui->music_btn->height());
            ui->music_btn->setIcon(nomusic);
            ui->music_btn->setFixedSize(ui->music_btn->size());
        }

        //主题
        QPixmap moon(":/imageUsed/moon.png");
        moon.scaled(ui->theme_btn->width(),ui->theme_btn->height());
        ui->theme_btn->setIcon(moon);
        ui->theme_btn->setFixedSize(ui->theme_btn->size());

        //修改密码
        QPixmap lockWhite(":/imageUsed/password_white.png");
        lockWhite.scaled(ui->update_btn->width(),ui->update_btn->height());
        ui->update_btn->setIcon(lockWhite);
        ui->update_btn->setFixedSize(ui->update_btn->size());

        //版本信息
        QPixmap aboutWhite(":/imageUsed/about_white.png");
        aboutWhite.scaled(ui->info->width(),ui->info->height());
        ui->info->setIcon(aboutWhite);
        ui->info->setFixedSize(ui->info->size());

        //退出登录
        QPixmap exitWhite(":/imageUsed/exit_white.png");
        exitWhite.scaled(ui->exit_btn->width(),ui->exit_btn->height());
        ui->exit_btn->setIcon(exitWhite);
        ui->exit_btn->setFixedSize(ui->exit_btn->size());
    }
    else if(!ui->theme_btn->isChecked())    //清新模式
    {
        //更改字体颜色
        ui->mainWindow->setStyleSheet("QLabel{color:black;}");
        ui->chartWindow->setStyleSheet("QLabel{color:black;}");
        ui->setWindow->setStyleSheet("QLabel{color:black;}");

        //日历
        {
            ui->calendarWidget->setStyleSheet("QCalendarWidget QTableView{"
                                              "alternate-background-color: rgb(206,230,238);"
                                              "background-color: rgb(206,230,238);"
                                              "Border: 1px solid rgb(89,99,99);}"
                                              "QToolButton#qt_calendar_monthbutton,#qt_calendar_yearbutton{"
                                              "color: #3A3A3A;}"
                                              "QCalendarWidget QWidget#qt_calendar_navigationbar{"
                                              "background-color: white;}");
        }

        //checkWindow按钮样式表
        ui->checkWindow->setStyleSheet("QPushButton{"
                                       "border:2px solid rgb(0,0,0);"
                                       "background: transparent;"
                                       "border-radius: 4px;"
                                       "color:black;}"
                                       "QPushButton:hover{"
                                       "background-color: rgb(169,171,181);"
                                       "color:white;"
                                       "border:2px solid rgb(255,255,255);}");

        //limitWindow按钮样式表
        ui->limitWindow->setStyleSheet("QPushButton{"
                                       "border:none;"
                                       "background: transparent;"
                                       "color:black;}"
                                       "QLabel{color:black;}");

        //返回主界面
        QPixmap back(":/imageUsed/back_blue.png");
        back.scaled(ui->pushButton->width(),ui->pushButton->height());
        ui->pushButton->setIcon(back);
        ui->pushButton->setFixedSize(ui->pushButton->size());

        //年份月份样式表
        ui->yearBox->setStyleSheet("QComboBox{"
                                   "border:2px solid rgb(0,122,255);"
                                   "background: transparent;"
                                   "border-radius: 4px;"
                                   "color:rgb(0,122,255);}"
                                   "QComboBox QAbstractItemView {background:rgb(204,228,237);"
                                   "color:rgb(0,122,255);}");
        ui->monthBox->setStyleSheet("QComboBox{"
                                    "border:2px solid rgb(0,122,255);"
                                    "background: transparent;"
                                    "border-radius: 4px;"
                                    "color:rgb(0,122,255);}"
                                    "QComboBox QAbstractItemView {background:rgb(204,228,237);"
                                    "color:rgb(0,122,255);}");

        //radioButton样式表
        ui->radioButton->setStyleSheet("QRadioButton{color:black;}");
        ui->radioButton_2->setStyleSheet("QRadioButton{color:black;}");
        ui->radioButton_3->setStyleSheet("QRadioButton{color:black;}");
        ui->radioButton_4->setStyleSheet("QRadioButton{color:black;}");
        ui->radioButton_5->setStyleSheet("QRadioButton{color:black;}");

        //tableview样式表
        ui->showTableView->setStyleSheet("QTableView{"
                                         //"Selection-background-color:"//选中条目的背景色
                                         "Background-color:transparent;"//整个表格的背景色
                                         "Border：1px solid rgb(169,171,181);}"
                                         );
        ui->editTableView->setStyleSheet("QTableView{"
                                         //"Selection-background-color:"//选中条目的背景色
                                         "Background-color:transparent;"//整个表格的背景色
                                         "Border：1px solid rgb(169,171,181);}"
                                         );

        //箭头
        ui->arrow->setPixmap(QPixmap(":/imageUsed/arrow_blue.png"));
        ui->arrow->setScaledContents(true);

        //温馨label
        ui->label_13->setStyleSheet("QLabel{color:rgb(0,101,210);}");

        //浅色背景
        ui->stackedWidget->setAutoFillBackground(true);
        QPalette palDark = ui->stackedWidget->palette();
        palDark.setBrush(QPalette::Window,
                         QBrush(QPixmap(":/imageUsed/background.png").scaled( ui->stackedWidget->size(),
                                                                              Qt::IgnoreAspectRatio,
                                                                              Qt::SmoothTransformation)));
        ui->stackedWidget->setPalette(palDark);

        //添加按钮
        QPixmap addBlue(":/imageUsed/add_blue.png");
        addBlue.scaled(ui->add_btn->width(),ui->add_btn->height());
        ui->add_btn->setIcon(addBlue);
        ui->add_btn->setFixedSize(ui->add_btn->size());

        //添加限额
        QPixmap setLimitBlue(":/imageUsed/featherPen_blue.png");
        setLimitBlue.scaled(ui->limit_btn->width(),ui->limit_btn->height());
        ui->limit_btn->setIcon(setLimitBlue);
        ui->limit_btn->setFixedSize(ui->limit_btn->size());

        //图表三个按钮
        QPixmap pieBlue(":/imageUsed/pie_blue.png");
        pieBlue.scaled(ui->pie_btn->width(),ui->pie_btn->height());
        ui->pie_btn->setIcon(pieBlue);
        ui->pie_btn->setFixedSize(ui->pie_btn->size());

        QPixmap barBlue(":/imageUsed/bar_blue.png");
        barBlue.scaled(ui->bar_btn->width(),ui->bar_btn->height());
        ui->bar_btn->setIcon(barBlue);
        ui->bar_btn->setFixedSize(ui->bar_btn->size());

        QPixmap lineBlue(":/imageUsed/line_blue.png");
        lineBlue.scaled(ui->line_btn->width(),ui->line_btn->height());
        ui->line_btn->setIcon(lineBlue);
        ui->line_btn->setFixedSize(ui->line_btn->size());

        //音效
        if(ui->music_btn->isChecked())
        {
            QPixmap music(":/imageUsed/nomusic_blue.png");
            music.scaled(ui->music_btn->width(),ui->music_btn->height());
            ui->music_btn->setIcon(music);
            ui->music_btn->setFixedSize(ui->music_btn->size());
        }
        else if(!ui->music_btn->isChecked())
        {
            QPixmap nomusic(":/imageUsed/music_blue.png");
            nomusic.scaled(ui->music_btn->width(),ui->music_btn->height());
            ui->music_btn->setIcon(nomusic);
            ui->music_btn->setFixedSize(ui->music_btn->size());
        }

        //主题
        QPixmap sun(":/imageUsed/sun_blue.png");
        sun.scaled(ui->theme_btn->width(),ui->theme_btn->height());
        ui->theme_btn->setIcon(sun);
        ui->theme_btn->setFixedSize(ui->theme_btn->size());

        //修改密码
        QPixmap lockBlue(":/imageUsed/password_blue.png");
        lockBlue.scaled(ui->update_btn->width(),ui->update_btn->height());
        ui->update_btn->setIcon(lockBlue);
        ui->update_btn->setFixedSize(ui->update_btn->size());

        //版本信息
        QPixmap aboutBlue(":/imageUsed/about_blue.png");
        aboutBlue.scaled(ui->info->width(),ui->info->height());
        ui->info->setIcon(aboutBlue);
        ui->info->setFixedSize(ui->info->size());

        //退出登录
        QPixmap exitBlue(":/imageUsed/exit_blue.png");
        exitBlue.scaled(ui->exit_btn->width(),ui->exit_btn->height());
        ui->exit_btn->setIcon(exitBlue);
        ui->exit_btn->setFixedSize(ui->exit_btn->size());
    }
}

//设置年份、月份下拉栏
void MainWindow::setYearMonthBox()
{
    //年份
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_year = current_date_time.toString("yyyy");
    int year = current_year.toInt();
    for (int i=year; i>=0; i--)
    {
        QString y = QString::number(i);
        ui->yearBox->addItem(y);
    }
    //月份
    QString current_month = current_date_time.toString("MM");
    int month = current_month.toInt();
    QStringList list;
    list<<"1月"<<"2月"<<"3月"<<"4月"<<"5月"<<"6月"<<"7月"<<"8月"<<"9月"<<"10月"<<"11月"<<"12月";
    ui->monthBox->addItems(list);
    ui->monthBox->setCurrentIndex(month-1);
}

//设置所用表名
void MainWindow::setUser(QString user)
{
    username=user;
    qDebug()<<username<<"登陆成功";
}

//打开版本信窗口
void MainWindow::showInfo()
{
    infoWindow *info=new infoWindow();
    info->setWindowModality(Qt::ApplicationModal);
    info->show();
}

//打开添加记账窗口
void MainWindow::showAdd()
{
    //设置为模态
    addWindow->setWindowModality(Qt::ApplicationModal);
    addWindow->setUser(username);
    addWindow->show();
}

//设置主界面标签
void MainWindow::setLabel()
{

    QDateTime current_date_time =QDateTime::currentDateTime();
    QString sql,str;
    int cur_year = current_date_time.toString("yyyy").toInt();
    int cur_month = current_date_time.toString("MM").toInt();
    int cur_day = current_date_time.toString("dd").toInt();
    //限额标签
    QSqlQuery query;
    sql=QString("select * from users where name = '%1'").arg(username);
    query.exec(sql);
    while(query.next())
    {
        if(query.value(3).toDouble()==NULL)
        {
            ui->label_4->setText(QString("日限额:无"));
        }
        else if(eachDaySumOut(cur_year,cur_month,cur_day)<(query.value(3).toDouble()*0.9))
        {
            ui->label_4->setText(QString("日限额:%1").arg(query.value(3).toDouble()));
        }
        else if(eachDaySumOut(cur_year,cur_month,cur_day)>=(query.value(3).toDouble()*0.9)
                &&eachDaySumOut(cur_year,cur_month,cur_day)<=query.value(3).toDouble())
        {
            ui->label_4->setText(QString("日限额:%1").arg(query.value(3).toDouble()));
        }
        else
        {
            ui->label_4->setText(QString("日限额:%1").arg(query.value(3).toDouble()));
        }
        if(query.value(4).toDouble()==NULL)
        {
            ui->label_5->setText(QString("月限额:无"));
        }
        else if(eachMonthSumOut(cur_year,cur_month)<(query.value(4).toDouble()*0.9))
        {
            ui->label_5->setText(QString("月限额:%1").arg(query.value(4).toDouble()));
        }
        else if(eachMonthSumOut(cur_year,cur_month)>=(query.value(4).toDouble()*0.9)
                &&eachMonthSumOut(cur_year,cur_month)<=query.value(4).toDouble())
        {
            ui->label_5->setText(QString("月限额:%1").arg(query.value(4).toDouble()));
        }
        else
        {
            ui->label_5->setText(QString("月限额:%1").arg(query.value(4).toDouble()));
        }
        if(query.value(5).toDouble()==NULL)
        {
            ui->label_6->setText(QString("年限额:无"));
        }
        else if(eachYearSumOut(cur_year)<(query.value(5).toDouble()*0.9))
        {
            ui->label_6->setText(QString("年限额:%1").arg(query.value(5).toDouble()));
        }
        else if(eachYearSumOut(cur_year)>=(query.value(5).toDouble()*0.9)
                &&eachYearSumOut(cur_year)<=query.value(5).toDouble())
        {
            ui->label_6->setText(QString("年限额:%1").arg(query.value(5).toDouble()));
        }
        else
        {
            ui->label_6->setText(QString("年限额:%1").arg(query.value(5).toDouble()));
        }
        ui->dayprogressBar->setValue(eachDaySumOut(cur_year,cur_month,cur_day));
        ui->monthprogressBar->setValue(eachMonthSumOut(cur_year,cur_month));
        ui->yearprogressBar->setValue(eachYearSumOut(cur_year));
        QString daylabel = QString("每日预算：%1/%2")
                .arg(eachDaySumOut(cur_year,cur_month,cur_day)).arg(query.value(3).toDouble());
        QString monthlabel = QString("每月预算：%1/%2")
                .arg(eachMonthSumOut(cur_year,cur_month)).arg(query.value(4).toDouble());
        QString yearlabel = QString("每年预算：%1/%2")
                .arg(eachYearSumOut(cur_year)).arg(query.value(5).toDouble());
        ui->dayBudgetSetlabel->setText(daylabel);
        ui->monthBudgetSetlabel->setText(monthlabel);
        ui->yearBudgetSetlabel->setText(yearlabel);
    }
    //显示标签
    str=QString("%1月%2日").arg(cur_month).arg(cur_day);
    ui->cur_month_day->setText(str);
    str=QString("%1月·支出").arg(cur_month);
    ui->label->setText(str);
    str=QString("%1").arg(eachMonthSumOut(cur_year,cur_month));
    ui->label_2->setText(str);
    str=QString("本月收入 %1").arg(eachMonthSumIn(cur_year,cur_month));
    ui->label_3->setText(str);
}

//限额弹窗判断
void MainWindow::judgeBudget()
{
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString sql;
    int cur_year = current_date_time.toString("yyyy").toInt();
    int cur_month = current_date_time.toString("MM").toInt();
    int cur_day = current_date_time.toString("dd").toInt();
    QSqlQuery query;
    sql=QString("select * from users where name = '%1'").arg(username);
    query.exec(sql);
    while(query.next())
    {
        if(query.value(3).toDouble()==NULL)
        {
            ui->dayprogressBar->setMaximum(0);
            ui->label_4->setText(QString("日限额:无"));
        }
        else if(eachDaySumOut(cur_year,cur_month,cur_day)<(query.value(3).toDouble()*0.9))
        {
            ui->dayprogressBar->setMaximum(query.value(3).toDouble());
            ui->label_4->setText(QString("日限额:%1").arg(query.value(3).toDouble()));
        }
        else if(eachDaySumOut(cur_year,cur_month,cur_day)>=(query.value(3).toDouble()*0.9)
                &&eachDaySumOut(cur_year,cur_month,cur_day)<=query.value(3).toDouble())
        {
            QMessageBox message(QMessageBox::NoIcon, "whoops！", "已达日限额90%，请合理消费!");
            //message.setIconPixmap(QPixmap(":/img_asset/icon_asset/2x_what the --.png"));
            message.exec();
            ui->dayprogressBar->setMaximum(query.value(3).toDouble());
            ui->label_4->setText(QString("日限额:%1").arg(query.value(3).toDouble()));
        }
        else
        {
            QMessageBox message(QMessageBox::NoIcon, "whoops！", "已超日限额，请合理消费!");
            //message.setIconPixmap(QPixmap(":/img_asset/icon_asset/2x_what the --.png"));
            message.exec();
            ui->dayprogressBar->setMaximum(eachDaySumOut(cur_year,cur_month,cur_day));
            ui->label_4->setText(QString("日限额:%1").arg(query.value(3).toDouble()));
        }
        if(query.value(4).toDouble()==NULL)
        {
            ui->monthprogressBar->setMaximum(0);
            ui->label_5->setText(QString("月限额:无"));
        }
        else if(eachMonthSumOut(cur_year,cur_month)<(query.value(4).toDouble()*0.9))
        {
            ui->monthprogressBar->setMaximum(query.value(4).toDouble());
            ui->label_5->setText(QString("月限额:%1").arg(query.value(4).toDouble()));
        }
        else if(eachMonthSumOut(cur_year,cur_month)>=(query.value(4).toDouble()*0.9)
                &&eachMonthSumOut(cur_year,cur_month)<=query.value(4).toDouble())
        {
            QMessageBox message(QMessageBox::NoIcon, "whoops！", "已达月限额90%，请合理消费!");
            //message.setIconPixmap(QPixmap(":/img_asset/icon_asset/2x_what the --.png"));
            message.exec();
            ui->monthprogressBar->setMaximum(query.value(4).toDouble());
            ui->label_5->setText(QString("月限额:%1").arg(query.value(4).toDouble()));
        }
        else
        {
            QMessageBox message(QMessageBox::NoIcon, "whoops！", "已超月限额，请合理消费!");
            //message.setIconPixmap(QPixmap(":/img_asset/icon_asset/2x_what the --.png"));
            message.exec();
            ui->monthprogressBar->setMaximum(eachMonthSumOut(cur_year,cur_month));
            ui->label_5->setText(QString("月限额:%1").arg(query.value(4).toDouble()));
        }
        if(query.value(5).toDouble()==NULL)
        {
            ui->yearprogressBar->setMaximum(0);
            ui->label_6->setText(QString("年限额:无"));
        }
        else if(eachYearSumOut(cur_year)<(query.value(5).toDouble()*0.9))
        {
            ui->yearprogressBar->setMaximum(query.value(5).toDouble());
            ui->label_6->setText(QString("年限额:%1").arg(query.value(5).toDouble()));
        }
        else if(eachYearSumOut(cur_year)>=(query.value(5).toDouble()*0.9)
                &&eachYearSumOut(cur_year)<=query.value(5).toDouble())
        {
            QMessageBox message(QMessageBox::NoIcon, "whoops！", "已达年限额90%，请合理消费!");
            //message.setIconPixmap(QPixmap(":/img_asset/icon_asset/2x_what the --.png"));
            message.exec();
            ui->yearprogressBar->setMaximum(query.value(5).toDouble());
            ui->label_6->setText(QString("年限额:%1").arg(query.value(5).toDouble()));
        }
        else
        {
            QMessageBox message(QMessageBox::NoIcon, "whoops！", "已超年限额，请合理消费!");
            //message.setIconPixmap(QPixmap(":/img_asset/icon_asset/2x_what the --.png"));
            message.exec();
            ui->yearprogressBar->setMaximum(eachYearSumOut(cur_year));
            ui->label_6->setText(QString("年限额:%1").arg(query.value(5).toDouble()));
        }
        ui->dayprogressBar->setValue(eachDaySumOut(cur_year,cur_month,cur_day));
        ui->monthprogressBar->setValue(eachMonthSumOut(cur_year,cur_month));
        ui->yearprogressBar->setValue(eachYearSumOut(cur_year));
        QString daylabel = QString("每日预算：%1/%2")
                .arg(eachDaySumOut(cur_year,cur_month,cur_day)).arg(query.value(3).toDouble());
        QString monthlabel = QString("每月预算：%1/%2")
                .arg(eachMonthSumOut(cur_year,cur_month)).arg(query.value(4).toDouble());
        QString yearlabel = QString("每年预算：%1/%2")
                .arg(eachYearSumOut(cur_year)).arg(query.value(5).toDouble());
        ui->dayBudgetSetlabel->setText(daylabel);
        ui->monthBudgetSetlabel->setText(monthlabel);
        ui->yearBudgetSetlabel->setText(yearlabel);
    }
}

//更改表中数据
void MainWindow::updateDatabase()
{
    QAbstractItemModel *Imodel=ui->editTableView->model();
    int row=ui->editTableView->currentIndex().row();
    int column=ui->editTableView->currentIndex().column();
    QModelIndex model_1 = Imodel->index(row,column);
    QModelIndex model_2 = Imodel->index(row,0);
    QModelIndex model_3 = Imodel->index(row,1);
    QVariant datatemp_1=Imodel->data(model_1);
    QVariant datatemp_2=Imodel->data(model_2);
    QVariant datatemp_3=Imodel->data(model_3);
    QSqlQuery query;
    QString sql;
    sql=QString("");
    if(column==1)
    {
        sql=QString("update %1 set in_out = '%2' where id = %3")
                .arg(username).arg(datatemp_1.toString()).arg(datatemp_2.toInt());
    }
    else if(column==2)
    {
        sql=QString("update %1 set money = %2 where id = %3")
                .arg(username).arg(datatemp_1.toDouble()).arg(datatemp_2.toInt());
    }
    else if(column==3)
    {
        sql=QString("update %1 set theme = '%2' where id = %3")
                .arg(username).arg(datatemp_1.toString()).arg(datatemp_2.toInt());
    }
    else if(column==4)
    {

        sql=QString("update %1 set date_year = %2 where id = %3")
                .arg(username).arg(datatemp_1.toInt()).arg(datatemp_2.toInt());
    }
    else if(column==5)
    {
        sql=QString("update %1 set date_month = %2 where id = %3")
                .arg(username).arg(datatemp_1.toInt()).arg(datatemp_2.toInt());
    }
    else if(column==6)
    {
        sql=QString("update %1 set date_day = %2 where id = %3")
                .arg(username).arg(datatemp_1.toInt()).arg(datatemp_2.toInt());
    }
    else if(column==7)
    {
        sql=QString("update %1 set mood = '%2' where id = %3")
                .arg(username).arg(datatemp_1.toString()).arg(datatemp_2.toInt());
    }
    else if(column==8)
    {
        sql=QString("update %1 set other = '%2' where id = %3")
                .arg(username).arg(datatemp_1.toString()).arg(datatemp_2.toInt());
    }
    query.exec(sql);
    showCashBook();
}

//更改密码
void MainWindow::changePassword()
{
    changePasswordWindow *changePw = new changePasswordWindow();
    changePw->setWindowModality(Qt::ApplicationModal);
    changePw->setUser(username);
    changePw->show();
}

//导出某日账单  文件名“xx-xxxx年xx月xx日账单”
void MainWindow::deriveFile(int year, int month, int day)
{
    QString fileName,fileCompleteName,sql,tableheader;
    QFile file;
    QSqlQuery query;
    fileName=QString("%4-%1年%2月%3日账单").arg(year).arg(month).arg(day).arg(username);
    fileCompleteName= QFileDialog::getSaveFileName(this,
                                                   tr("保存表格"),
                                                   fileName+ ".csv",
                                                   tr("csv File(*.csv)"));
    sql=QString("select * from %1 where date_year = '%2' and date_month = '%3' and date_day = '%4'")
            .arg(username).arg(year).arg(month).arg(day);
    query.exec(sql);
    if(fileCompleteName.isNull())
    {
        return;
    }
    file.setFileName(fileCompleteName);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug()<<"open file fail";
        return;
    }
    //创建一个文本流，向保存文件中写入文本
    QTextStream out(&file);
    //写表头
    tableheader=QString(u8"序号,收入/支出,金额,分类,年,月,日,时,分,心情,备注");
    out<<tableheader;
    out<<"\n";
    //写数据
    while(query.next())
    {
        for(int i=0;i<11;i++)
        {
            out<<query.value(i).toString()<<",";
        }
        out<<"\n";
    }
    file.close();
}

//导出某月账单  文件名“xx-xxxx年xx月账单”
void MainWindow::deriveFile(int year, int month)
{
    QString fileName,fileCompleteName,sql,tableheader;
    QSqlQuery query;
    QFile file;
    fileName=QString("%3-%1年%2月账单").arg(year).arg(month).arg(username);
    fileCompleteName= QFileDialog::getSaveFileName(this,
                                                   tr("保存表格"),
                                                   fileName+ ".csv",
                                                   tr("csv File(*.csv)"));
    sql=QString("select * from %1 where date_year = '%2' and date_month = '%3'")
            .arg(username).arg(year).arg(month);
    query.exec(sql);
    if(fileCompleteName.isNull())
    {
        return;
    }
    file.setFileName(fileCompleteName);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug()<<"open file fail";
        return;
    }
    //创建一个文本流，向保存文件中写入文本
    QTextStream out(&file);
    //写表头
    tableheader=QString(u8"序号,收入/支出,金额,分类,年,月,日,时,分,心情,备注");
    out<<tableheader;
    out<<"\n";
    //写数据
    while(query.next())
    {
        for(int i=0;i<11;i++)
        {
            out<<query.value(i).toString()<<",";
        }
        out<<"\n";
    }
    file.close();
}

//导出某年账单  文件名“xx-xxxx年账单”
void MainWindow::deriveFile(int year)
{
    QString fileName,fileCompleteName,sql,tableheader;
    QSqlQuery query;
    QFile file;
    fileName=QString("%2-%1年账单").arg(year).arg(username);
    fileCompleteName= QFileDialog::getSaveFileName(this,
                                                   tr("保存表格"),
                                                   fileName+ ".csv",
                                                   tr("csv File(*.csv)"));
    sql=QString("select * from %1 where date_year = '%2'")
            .arg(username).arg(year);
    query.exec(sql);
    if(fileCompleteName.isNull())
    {
        return;
    }
    file.setFileName(fileCompleteName);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug()<<"open file fail";
        return;
    }
    //创建一个文本流，向保存文件中写入文本
    QTextStream out(&file);
    //写表头
    tableheader=QString(u8"序号,收入/支出,金额,分类,年,月,日,时,分,心情,备注");
    out<<tableheader;
    out<<"\n";
    //写数据
    while(query.next())
    {
        for(int i=0;i<11;i++)
        {
            out<<query.value(i).toString()<<",";
        }
        out<<"\n";
    }
    file.close();
}

//导出文件
void MainWindow::derive()
{
    QDate date=ui->calendarWidget->selectedDate();
    int day=date.day();
    int month=date.month();
    int year=date.year();
    if(ui->radioButton->isChecked())
    {
        deriveFile(year,month,day);
    }
    else if(ui->radioButton_2->isChecked())
    {
        deriveFile(year,month);
    }
    else if(ui->radioButton_3->isChecked())
    {
        deriveFile(year);
    }
}

//绘制图表
void MainWindow::chartShow()
{
    int year=ui->yearBox->currentText().toInt();
    int month=(ui->monthBox->currentIndex())+1;
    if(ui->radioButton_5->isChecked())
        ui->chartView->setCurrentWidget(chartPieWidget(year,month));
    else
        ui->chartView->setCurrentWidget(chartPieWidget(year));
}

//某类某月总支出
double MainWindow::themeSumOut(int year, int month, QString theme)
{
    QString sql;
    QSqlQuery query;
    double sum=0.0;
    sql=QString("select * from %1 where date_year = '%2' and date_month = '%3' and theme = '%4' and in_out = '支出'")
            .arg(username).arg(year).arg(month).arg(theme);
    query.exec(sql);
    while(query.next())
    {
        sum+=query.value(2).toDouble();
    }
    return sum;
}

//某类某年总支出
double MainWindow::themeSumOut(int year, QString theme)
{
    QString sql;
    QSqlQuery query;
    double sum=0.0;
    sql=QString("select * from %1 where date_year = '%2' and theme = '%3' and in_out = '支出'")
            .arg(username).arg(year).arg(theme);
    query.exec(sql);
    while(query.next())
    {
        sum+=query.value(2).toDouble();
    }
    return sum;
}

//某天总收入
double MainWindow::eachDaySumIn(int year, int month, int day)
{
    QString sql;
    QSqlQuery query;
    double sum=0.0;
    sql=QString("select * from %1 where date_year = '%2' and date_month = '%3' and date_day = '%4' and in_out = '收入'")
            .arg(username).arg(year).arg(month).arg(day);
    query.exec(sql);
    while(query.next())
    {
        sum+=query.value(2).toDouble();
    }
    return sum;
}

//某天总支出
double MainWindow::eachDaySumOut(int year, int month, int day)
{
    QString sql;
    QSqlQuery query;
    double sum=0.0;
    sql=QString("select * from %1 where date_year = '%2' and date_month = '%3' and date_day = '%4' and in_out = '支出'")
            .arg(username).arg(year).arg(month).arg(day);
    query.exec(sql);
    while(query.next())
    {
        sum+=query.value(2).toDouble();
    }
    return sum;
}

//某月总收入
double MainWindow::eachMonthSumIn(int year, int month)
{
    QString sql;
    QSqlQuery query;
    double sum=0.0;
    sql=QString("select * from %1 where date_year = '%2' and date_month = '%3' and in_out = '收入'")
            .arg(username).arg(year).arg(month);
    query.exec(sql);
    while(query.next())
    {
        sum+=query.value(2).toDouble();
    }
    return sum;
}

//某月总支出
double MainWindow::eachMonthSumOut(int year, int month)
{
    QString sql;
    QSqlQuery query;
    double sum=0.0;
    sql=QString("select * from %1 where date_year = '%2' and date_month = '%3' and in_out = '支出'")
            .arg(username).arg(year).arg(month);
    query.exec(sql);
    while(query.next())
    {
        sum+=query.value(2).toDouble();
    }
    return sum;
}

//某年总支出
double MainWindow::eachYearSumOut(int year)
{
    double yearMoney=themeSumOut(year, "食品酒水")+themeSumOut(year, "医疗保健")
            +themeSumOut(year, "衣服饰品")+themeSumOut(year, "交流通讯")
            +themeSumOut(year, "行车交通")+themeSumOut(year, "寝室费用")
            +themeSumOut(year, "学习进修")+themeSumOut(year, "休闲娱乐")
            +themeSumOut(year, "其他");
    return yearMoney;
}

//年饼状图
QWidget* MainWindow::chartPieWidget(int year)
{
    QChart *m_chart = new QChart();
    QPieSeries *series = new QPieSeries();
    if((themeSumOut(year,"食品酒水")==0)&&(themeSumOut(year,"衣服饰品")==0)
            &&(themeSumOut(year,"交流通讯")==0)&&(themeSumOut(year,"行车交通")==0)
            &&(themeSumOut(year,"寝室费用")==0)&&(themeSumOut(year,"学习进修")==0)
            &&(themeSumOut(year,"休闲娱乐")==0)&&(themeSumOut(year,"医疗保健")==0)
            &&(themeSumOut(year,"其他")==0))
    {
        series->append(QString("未消费"),1);
        series->slices().at(0)->setColor(QColor(13,128,217));   //设置颜色
        series->slices().at(0)->setLabelColor(QColor(13,128,217));
        series->setLabelsVisible(true);
        series->setUseOpenGL(true);
    }
    else
    {
        if(themeSumOut(year,"食品酒水")!=0)
        {
            double per1=themeSumOut(year,"食品酒水")/eachYearSumOut(year)*100;
            QString s1=QString::number(per1,'f',2);
            series->append(QString("食品酒水:"+s1+"%"),themeSumOut(year, "食品酒水"));      //添加标签
        }
        if(themeSumOut(year,"衣服饰品")!=0)
        {
            double per2=themeSumOut(year,"衣服饰品")/eachYearSumOut(year)*100;
            QString s2=QString::number(per2,'f',2);
            series->append(QString("衣服饰品:"+s2+"%"),themeSumOut(year, "衣服饰品"));
        }
        if(themeSumOut(year,"交流通讯")!=0)
        {
            double per3=themeSumOut(year,"交流通讯")/eachYearSumOut(year)*100;
            QString s3=QString::number(per3,'f',2);
            series->append(QString("交流通讯:"+s3+"%"),themeSumOut(year, "交流通讯"));
        }
        if(themeSumOut(year,"行车交通")!=0)
        {
            double per4=themeSumOut(year,"行车交通")/eachYearSumOut(year)*100;
            QString s4=QString::number(per4,'f',2);
            series->append(QString("行车交通:"+s4+"%"),themeSumOut(year, "行车交通"));
        }
        if(themeSumOut(year,"寝室费用")!=0)
        {
            double per5=themeSumOut(year,"寝室费用")/eachYearSumOut(year)*100;
            QString s5=QString::number(per5,'f',2);
            series->append(QString("寝室费用:"+s5+"%"),themeSumOut(year, "寝室费用"));
        }
        if(themeSumOut(year,"学习进修")!=0)
        {
            double per6=themeSumOut(year,"学习进修")/eachYearSumOut(year)*100;
            QString s6=QString::number(per6,'f',2);
            series->append(QString("学习进修:"+s6+"%"),themeSumOut(year, "学习进修"));
        }
        if(themeSumOut(year,"休闲娱乐")!=0)
        {
            double per7=themeSumOut(year,"休闲娱乐")/eachYearSumOut(year)*100;
            QString s7=QString::number(per7,'f',2);
            series->append(QString("休闲娱乐:"+s7+"%"),themeSumOut(year, "休闲娱乐"));
        }
        if(themeSumOut(year,"医疗保健")!=0)
        {
            double per8=themeSumOut(year,"医疗保健")/eachYearSumOut(year)*100;
            QString s8=QString::number(per8,'f',2);
            series->append(QString("医疗保健:"+s8+"%"),themeSumOut(year, "医疗保健"));
        }
        if(themeSumOut(year,"其他")!=0)
        {
            double per9=themeSumOut(year,"其他")/eachYearSumOut(year)*100;
            QString s9=QString::number(per9,'f',2);
            series->append(QString("其他:"+s9+"%"),themeSumOut(year, "其他"));
        }

        series->setLabelsVisible(true);
        series->setUseOpenGL(true);
    }
    if(ui->theme_btn->isChecked())
        m_chart->setTheme(QChart::ChartThemeBlueCerulean);//设置深色主题
    else
        m_chart->setTheme(QChart::ChartThemeLight);//设置白色主题
    m_chart->setDropShadowEnabled(true);//背景阴影
    m_chart->addSeries(series);//添加系列到QChart上
    m_chart->setTitle("饼状图");
    //修改说明图例样式
    m_chart->legend()->hide();
    m_chart->setAnimationOptions(QChart::AllAnimations);
    QChartView *chartViewPie = new QChartView(m_chart);//QChartView 一步到位直接显示QChart
    chartViewPie->setRenderHint(QPainter::Antialiasing);//继承来的抗锯齿方法
    //饼形图窗口
    QGridLayout *baseLayoutPie = new QGridLayout();
    baseLayoutPie->addWidget(chartViewPie, 0, 0);
    setLayout(baseLayoutPie);
    QGridLayout *vlayoutPie = new QGridLayout;
    vlayoutPie->addLayout(baseLayoutPie, 0, 0);
    QWidget *widgetPie = new QWidget;
    widgetPie->setLayout(vlayoutPie);
    //加入stackedWidget
    ui->chartView->addWidget(widgetPie);
    return widgetPie;
}

//月饼状图
QWidget* MainWindow::chartPieWidget(int year,int month)
{
    QChart *m_chart = new QChart();
    QPieSeries *series = new QPieSeries();

    if((themeSumOut(year, month,"食品酒水")==0)&&(themeSumOut(year, month,"衣服饰品")==0)
            &&(themeSumOut(year, month,"交流通讯")==0)&&(themeSumOut(year, month,"行车交通")==0)
            &&(themeSumOut(year, month,"寝室费用")==0)&&(themeSumOut(year, month,"学习进修")==0)
            &&(themeSumOut(year, month,"休闲娱乐")==0)&&(themeSumOut(year, month,"医疗保健")==0)
            &&(themeSumOut(year, month,"其他")==0))
    {
        series->append(QString("未消费"),1);
        series->slices().at(0)->setColor(QColor(13,128,217));   //设置颜色
        series->slices().at(0)->setLabelColor(QColor(13,128,217));
        series->setLabelsVisible(true);
        series->setUseOpenGL(true);
    }
    else
    {
        if(themeSumOut(year, month,"食品酒水")!=0)
        {
            double per1=themeSumOut(year, month,"食品酒水")/eachMonthSumOut(year,month)*100;
            QString s1=QString::number(per1,'f',2);
            series->append(QString("食品酒水:"+s1+"%"),themeSumOut(year, month, "食品酒水"));      //添加标签
        }
        if(themeSumOut(year, month,"衣服饰品")!=0)
        {
            double per2=themeSumOut(year, month,"衣服饰品")/eachMonthSumOut(year,month)*100;
            QString s2=QString::number(per2,'f',2);
            series->append(QString("衣服饰品:"+s2+"%"),themeSumOut(year, month, "衣服饰品"));
        }
        if(themeSumOut(year, month,"交流通讯")!=0)
        {
            double per3=themeSumOut(year, month,"交流通讯")/eachMonthSumOut(year,month)*100;
            QString s3=QString::number(per3,'f',2);
            series->append(QString("交流通讯:"+s3+"%"),themeSumOut(year, month, "交流通讯"));
        }
        if(themeSumOut(year, month,"行车交通")!=0)
        {
            double per4=themeSumOut(year, month,"行车交通")/eachMonthSumOut(year,month)*100;
            QString s4=QString::number(per4,'f',2);
            series->append(QString("行车交通:"+s4+"%"),themeSumOut(year, month, "行车交通"));
        }
        if(themeSumOut(year, month,"寝室费用")!=0)
        {
            double per5=themeSumOut(year, month,"寝室费用")/eachMonthSumOut(year,month)*100;
            QString s5=QString::number(per5,'f',2);
            series->append(QString("寝室费用:"+s5+"%"),themeSumOut(year, month, "寝室费用"));
        }
        if(themeSumOut(year, month,"学习进修")!=0)
        {
            double per6=themeSumOut(year, month,"学习进修")/eachMonthSumOut(year,month)*100;
            QString s6=QString::number(per6,'f',2);
            series->append(QString("学习进修:"+s6+"%"),themeSumOut(year, month, "学习进修"));
        }
        if(themeSumOut(year, month,"休闲娱乐")!=0)
        {
            double per7=themeSumOut(year, month,"休闲娱乐")/eachMonthSumOut(year,month)*100;
            QString s7=QString::number(per7,'f',2);
            series->append(QString("休闲娱乐:"+s7+"%"),themeSumOut(year, month, "休闲娱乐"));
        }
        if(themeSumOut(year, month,"医疗保健")!=0)
        {
            double per8=themeSumOut(year, month,"医疗保健")/eachMonthSumOut(year,month)*100;
            QString s8=QString::number(per8,'f',2);
            series->append(QString("医疗保健:"+s8+"%"),themeSumOut(year, month, "医疗保健"));
        }
        if(themeSumOut(year, month,"其他")!=0)
        {
            double per9=themeSumOut(year, month,"其他")/eachMonthSumOut(year,month)*100;
            QString s9=QString::number(per9,'f',2);
            series->append(QString("其他:"+s9+"%"),themeSumOut(year, month, "其他"));
        }
        series->setLabelsVisible(true);
        series->setUseOpenGL(true);
    }
    if(ui->theme_btn->isChecked())
        m_chart->setTheme(QChart::ChartThemeBlueCerulean);//设置深色主题
    else
        m_chart->setTheme(QChart::ChartThemeLight);//设置白色主题
    m_chart->setDropShadowEnabled(true);//背景阴影
    m_chart->addSeries(series);//添加系列到QChart上
    m_chart->setTitle("饼状图");
    //修改说明样式
    m_chart->legend()->hide();
    m_chart->setAnimationOptions(QChart::AllAnimations);
    QChartView *chartViewPie = new QChartView(m_chart);//QChartView 一步到位直接显示QChart
    chartViewPie->setRenderHint(QPainter::Antialiasing);//继承来的抗锯齿方法
    //饼形图窗口
    QGridLayout *baseLayoutPie = new QGridLayout();
    baseLayoutPie->addWidget(chartViewPie, 0, 0);
    setLayout(baseLayoutPie);
    QGridLayout *vlayoutPie = new QGridLayout;
    vlayoutPie->addLayout(baseLayoutPie, 0, 0);
    QWidget *widgetPie = new QWidget;
    widgetPie->setLayout(vlayoutPie);
    //加入stackedWidget
    ui->chartView->addWidget(widgetPie);
    return widgetPie;
}

//年柱状图
QWidget* MainWindow::chartBarWidget(int year)
{
    QChart *m_chartBar = new QChart();
    //创建条线数据
    QBarSet *set0 = new QBarSet("总支出");
    //设置条形数据颜色
    set0->setLabelColor(QColor(11,60,92));
    //添加消费数据
    *set0 << themeSumOut(year,"食品酒水")
          << themeSumOut(year,"衣服饰品")
          << themeSumOut(year,"交流通讯")
          << themeSumOut(year,"行车交通")
          << themeSumOut(year,"寝室费用")
          << themeSumOut(year,"学习进修")
          << themeSumOut(year,"休闲娱乐")
          << themeSumOut(year,"医疗保健")
          << themeSumOut(year,"其他");

    QHorizontalBarSeries *seriesBar = new QHorizontalBarSeries();
    seriesBar->append(set0);
    seriesBar->setVisible(true);
    seriesBar->setLabelsVisible(true);
    if(ui->theme_btn->isChecked())
        m_chartBar->setTheme(QChart::ChartThemeBlueCerulean);//设置深色主题
    else
        m_chartBar->setTheme(QChart::ChartThemeLight);//设置白色主题
    m_chartBar->setDropShadowEnabled(true);//背景阴影
    m_chartBar->addSeries(seriesBar);//添加系列到QChart上
    m_chartBar->setTitle("柱形图");
    //创建X轴和Y轴
    QBarCategoryAxis *axisXBar = new QBarCategoryAxis;
    axisXBar->append("食品酒水");
    axisXBar->append("衣服饰品");
    axisXBar->append("交流通讯");
    axisXBar->append("行车交通");
    axisXBar->append("寝室费用");
    axisXBar->append("学习进修");
    axisXBar->append("休闲娱乐");
    axisXBar->append("医疗保健");
    axisXBar->append("其他");
    QValueAxis *axisYBar = new QValueAxis;
    if((themeSumOut(year,"食品酒水")==0)&&(themeSumOut(year,"衣服饰品")==0)
            &&(themeSumOut(year,"交流通讯")==0)&&(themeSumOut(year,"行车交通")==0)
            &&(themeSumOut(year,"寝室费用")==0)&&(themeSumOut(year,"学习进修")==0)
            &&(themeSumOut(year,"休闲娱乐")==0)&&(themeSumOut(year,"医疗保健")==0)
            &&(themeSumOut(year,"其他")==0))
        axisYBar->setRange(0,100);
    axisYBar->setTitleText("金额");
    axisYBar->setLabelFormat("%.2f");
    m_chartBar->setAxisX(axisYBar,seriesBar);
    m_chartBar->setAxisY(axisXBar,seriesBar);
    //修改说明样式
    m_chartBar->legend()->setVisible(true);
    m_chartBar->legend()->setAlignment(Qt::AlignBottom);//底部对齐
    m_chartBar->legend()->setBackgroundVisible(true);//设置背景是否可视
    m_chartBar->legend()->setAutoFillBackground(true);//设置背景自动填充
    m_chartBar->legend()->setMaximumHeight(50);
    m_chartBar->setAnimationOptions(QChart::AllAnimations);
    QChartView *chartViewBar= new QChartView(m_chartBar);
    chartViewBar->setRenderHint(QPainter::Antialiasing);
    //柱状图窗口
    QGridLayout *baseLayoutBar = new QGridLayout();
    baseLayoutBar->addWidget(chartViewBar, 1, 0);
    setLayout(baseLayoutBar);
    QGridLayout *vlayoutBar = new QGridLayout;
    vlayoutBar->addLayout(baseLayoutBar, 0, 0);
    QWidget *widgetBar = new QWidget;
    widgetBar->setLayout(vlayoutBar);
    //柱状图添加到stackedWidget
    ui->chartView->addWidget(widgetBar);
    return widgetBar;
}

//月柱状图
QWidget* MainWindow::chartBarWidget(int year,int month)
{
    QChart *m_chartBar = new QChart();
    //创建条线数据
    QBarSet *set0 = new QBarSet("总支出");
    //设置条形数据颜色
    set0->setLabelColor(QColor(11,60,92));
    //添加消费数据
    *set0 << themeSumOut(year,month,"食品酒水")
          << themeSumOut(year,month,"衣服饰品")
          << themeSumOut(year,month,"交流通讯")
          << themeSumOut(year,month,"行车交通")
          << themeSumOut(year,month,"寝室费用")
          << themeSumOut(year,month,"学习进修")
          << themeSumOut(year,month,"休闲娱乐")
          << themeSumOut(year,month,"医疗保健")
          << themeSumOut(year,month,"其他");

    QHorizontalBarSeries *seriesBar = new QHorizontalBarSeries();
    seriesBar->append(set0);
    seriesBar->setVisible(true);
    seriesBar->setLabelsVisible(true);
    if(ui->theme_btn->isChecked())
        m_chartBar->setTheme(QChart::ChartThemeBlueCerulean);//设置深色主题
    else
        m_chartBar->setTheme(QChart::ChartThemeLight);//设置白色主题
    m_chartBar->setDropShadowEnabled(true);//背景阴影
    m_chartBar->addSeries(seriesBar);//添加系列到QChart上
    m_chartBar->setTitle("柱形图");
    //创建X轴和Y轴
    QBarCategoryAxis *axisXBar = new QBarCategoryAxis;
    axisXBar->append("食品酒水");
    axisXBar->append("衣服饰品");
    axisXBar->append("交流通讯");
    axisXBar->append("行车交通");
    axisXBar->append("寝室费用");
    axisXBar->append("学习进修");
    axisXBar->append("休闲娱乐");
    axisXBar->append("医疗保健");
    axisXBar->append("其他");
    QValueAxis *axisYBar = new QValueAxis;
    if((themeSumOut(year, month,"食品酒水")==0)&&(themeSumOut(year, month,"衣服饰品")==0)
            &&(themeSumOut(year, month,"交流通讯")==0)&&(themeSumOut(year, month,"行车交通")==0)
            &&(themeSumOut(year, month,"寝室费用")==0)&&(themeSumOut(year, month,"学习进修")==0)
            &&(themeSumOut(year, month,"休闲娱乐")==0)&&(themeSumOut(year, month,"医疗保健")==0)
            &&(themeSumOut(year, month,"其他")==0))
        axisYBar->setRange(0,100);
    axisYBar->setTitleText("金额");
    axisYBar->setLabelFormat("%.2f");
    m_chartBar->setAxisX(axisYBar,seriesBar);
    m_chartBar->setAxisY(axisXBar,seriesBar);
    //修改说明样式
    m_chartBar->legend()->setVisible(true);
    m_chartBar->legend()->setAlignment(Qt::AlignBottom);//底部对齐
    m_chartBar->legend()->setBackgroundVisible(true);//设置背景是否可视
    m_chartBar->legend()->setAutoFillBackground(true);//设置背景自动填充
    m_chartBar->legend()->setMaximumHeight(50);
    m_chartBar->setAnimationOptions(QChart::AllAnimations);
    QChartView *chartViewBar= new QChartView(m_chartBar);
    chartViewBar->setRenderHint(QPainter::Antialiasing);
    //柱状图窗口
    QGridLayout *baseLayoutBar = new QGridLayout();
    baseLayoutBar->addWidget(chartViewBar, 1, 0);
    setLayout(baseLayoutBar);
    QGridLayout *vlayoutBar = new QGridLayout;
    vlayoutBar->addLayout(baseLayoutBar, 0, 0);
    QWidget *widgetBar = new QWidget;
    widgetBar->setLayout(vlayoutBar);
    //柱状图添加到stackedWidget
    ui->chartView->addWidget(widgetBar);
    return widgetBar;
}

//年折线图
QWidget* MainWindow::chartLineWidget(int year)
{
    QList<QLineSeries *> m_series;
    QLineSeries *series1 = new QLineSeries();//实例化一个QLineSeries对象
    QLineSeries *series2 = new QLineSeries();//实例化一个QLineSeries对象
    m_series.append(series1);
    m_series.append(series2);
    //设置线条名称
    series1->setName(QString("总收入"));
    series2->setName(QString("总支出"));
    //设置是否线条可视
    series1->setVisible(true);
    series2->setVisible(true);
    series1->setPointsVisible(true);
    series2->setPointsVisible(true);

    //    //创建X轴和Y轴
    QValueAxis *axisXLine = new QValueAxis;
    axisXLine->setRange(1,12);

    axisXLine->setLabelsColor(QColor(0,0,0));

    QValueAxis *axisYLine = new QValueAxis;
    axisYLine->setTitleText("金额");
    axisYLine->setLabelFormat("%.2f");
    axisXLine->setTitleText("月份");
    axisXLine->setLabelFormat("%.0f");
    axisXLine->setTickCount(12);

    //添加坐标点
    if((eachMonthSumIn(year,1)==0)&&(eachMonthSumIn(year,2)==0)
            &&(eachMonthSumIn(year,3)==0)&&(eachMonthSumIn(year,4)==0)
            &&(eachMonthSumIn(year,5)==0)&&(eachMonthSumIn(year,6)==0)
            &&(eachMonthSumIn(year,7)==0)&&(eachMonthSumIn(year,8)==0)
            &&(eachMonthSumIn(year,9)==0)&&(eachMonthSumIn(year,10)==0)
            &&(eachMonthSumIn(year,11)==0)&&(eachMonthSumIn(year,12)==0)
            &&(eachMonthSumOut(year,1)==0)&&(eachMonthSumOut(year,2)==0)
            &&(eachMonthSumOut(year,3)==0)&&(eachMonthSumOut(year,4)==0)
            &&(eachMonthSumOut(year,5)==0)&&(eachMonthSumOut(year,6)==0)
            &&(eachMonthSumOut(year,7)==0)&&(eachMonthSumOut(year,8)==0)
            &&(eachMonthSumOut(year,9)==0)&&(eachMonthSumOut(year,10)==0)
            &&(eachMonthSumOut(year,11)==0)&&(eachMonthSumOut(year,12)==0))
    {
        axisYLine->setRange(0,10);
        for(int i=1;i<=12;i++)
        {
            series1->append(i,0);
        }
        for(int i=1;i<=12;i++)
        {
            series2->append(i,0);
        }
    }
    else
    {
        for(int i=1;i<=12;i++)
        {
            series1->append(i,eachMonthSumIn(year,i));
        }
        for(int i=1;i<=12;i++)
        {
            series2->append(i,eachMonthSumOut(year,i));
        }
    }

    QChart *m_chartLine = new QChart();
    if(ui->theme_btn->isChecked())
        m_chartLine->setTheme(QChart::ChartThemeBlueCerulean);//设置深色主题
    else
        m_chartLine->setTheme(QChart::ChartThemeLight);//设置系统主题
    m_chartLine->setAnimationOptions(QChart::AllAnimations);//设置启用或禁用动画
    //chart->setBackgroundBrush(QBrush(QColor(170,170,255)));//设置背景色,主题和背景二选一
    m_chartLine->setDropShadowEnabled(true);//是否背景阴影
    m_chartLine->setLocalizeNumbers(true);//数字是否本地化
    m_chartLine->legend()->show();//legend是否显示，show和hide
    m_chartLine->addSeries(series1);//添加系列到QChart上
    m_chartLine->addSeries(series2);//添加系列到QChart上
    m_chartLine->createDefaultAxes();//创建默认轴

    m_chartLine->setAxisX(axisXLine,series1);
    m_chartLine->setAxisX(axisXLine,series2);
    m_chartLine->setAxisY(axisYLine,series1);
    m_chartLine->setAxisY(axisYLine,series2);

    axisXLine->setGridLineVisible(true); //设置是否显示网格线
    axisYLine->setGridLineVisible(true); //设置是否显示网格线
    m_chartLine->setTitle("折线图");//设置标题

    m_chartLine->legend()->setAlignment(Qt::AlignRight);//右边对齐
    m_chartLine->legend()->setBackgroundVisible(true);//设置背景是否可视
    m_chartLine->legend()->setVisible(true);//设置是否可视
    QChartView *chartViewLine = new QChartView(m_chartLine);
    chartViewLine->setRenderHint(QPainter::Antialiasing);
    //折线图窗口
    QGridLayout *baseLayoutLine = new QGridLayout();
    baseLayoutLine->addWidget(chartViewLine, 1, 0);
    setLayout(baseLayoutLine);
    QGridLayout *vlayoutLine = new QGridLayout;
    vlayoutLine->addLayout(baseLayoutLine, 0, 0);
    QWidget *widgetLine = new QWidget;
    widgetLine->setLayout(vlayoutLine);
    //折线图添加到stackedWidget
    ui->chartView->addWidget(widgetLine);
    return widgetLine;
}

//月折线图
QWidget* MainWindow::chartLineWidget(int year,int month)
{
    QList<QLineSeries *> m_series;
    QLineSeries *series1 = new QLineSeries();//实例化一个QLineSeries对象
    QLineSeries *series2 = new QLineSeries();//实例化一个QLineSeries对象
    m_series.append(series1);
    m_series.append(series2);
    //设置线条名称
    series1->setName(QString("总收入"));
    series2->setName(QString("总支出"));
    //设置是否线条可视
    series1->setVisible(true);
    series2->setVisible(true);
    //设置点标签是否可视
    series1->setPointsVisible(true);
    series2->setPointsVisible(true);

    //创建X轴和Y轴
    QValueAxis *axisXLine = new QValueAxis;
    axisXLine->setLabelsColor(QColor(0,0,0));
    QValueAxis *axisYLine = new QValueAxis;
    axisYLine->setTitleText("金额");
    axisYLine->setLabelFormat("%.2f");
    axisXLine->setTitleText("日期");
    axisXLine->setLabelFormat("%.0f");

    //添加坐标点
    if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
    {
        axisXLine->setRange(1,31);
        axisXLine->setTickCount(6);
        if((eachDaySumIn(year,month,1)==0)&&(eachDaySumIn(year,month,2)==0)
                &&(eachDaySumIn(year,month,3)==0)&&(eachDaySumIn(year,month,4)==0)
                &&(eachDaySumIn(year,month,5)==0)&&(eachDaySumIn(year,month,6)==0)
                &&(eachDaySumIn(year,month,7)==0)&&(eachDaySumIn(year,month,8)==0)
                &&(eachDaySumIn(year,month,9)==0)&&(eachDaySumIn(year,month,10)==0)
                &&(eachDaySumIn(year,month,11)==0)&&(eachDaySumIn(year,month,12)==0)
                &&(eachDaySumIn(year,month,13)==0)&&(eachDaySumIn(year,month,14)==0)
                &&(eachDaySumIn(year,month,15)==0)&&(eachDaySumIn(year,month,16)==0)
                &&(eachDaySumIn(year,month,17)==0)&&(eachDaySumIn(year,month,18)==0)
                &&(eachDaySumIn(year,month,19)==0)&&(eachDaySumIn(year,month,20)==0)
                &&(eachDaySumIn(year,month,21)==0)&&(eachDaySumIn(year,month,22)==0)
                &&(eachDaySumIn(year,month,23)==0)&&(eachDaySumIn(year,month,24)==0)
                &&(eachDaySumIn(year,month,25)==0)&&(eachDaySumIn(year,month,26)==0)
                &&(eachDaySumIn(year,month,27)==0)&&(eachDaySumIn(year,month,28)==0)
                &&(eachDaySumIn(year,month,29)==0)&&(eachDaySumIn(year,month,30)==0)
                &&(eachDaySumIn(year,month,31)==0)&&(eachDaySumOut(year,month,1)==0)&&(eachDaySumIn(year,month,2)==0)
                &&(eachDaySumOut(year,month,3)==0)&&(eachDaySumOut(year,month,4)==0)
                &&(eachDaySumOut(year,month,5)==0)&&(eachDaySumOut(year,month,6)==0)
                &&(eachDaySumOut(year,month,7)==0)&&(eachDaySumOut(year,month,8)==0)
                &&(eachDaySumOut(year,month,9)==0)&&(eachDaySumOut(year,month,10)==0)
                &&(eachDaySumOut(year,month,11)==0)&&(eachDaySumOut(year,month,12)==0)
                &&(eachDaySumOut(year,month,13)==0)&&(eachDaySumOut(year,month,14)==0)
                &&(eachDaySumOut(year,month,15)==0)&&(eachDaySumOut(year,month,16)==0)
                &&(eachDaySumOut(year,month,17)==0)&&(eachDaySumOut(year,month,18)==0)
                &&(eachDaySumOut(year,month,19)==0)&&(eachDaySumOut(year,month,20)==0)
                &&(eachDaySumOut(year,month,21)==0)&&(eachDaySumOut(year,month,22)==0)
                &&(eachDaySumOut(year,month,23)==0)&&(eachDaySumOut(year,month,24)==0)
                &&(eachDaySumOut(year,month,25)==0)&&(eachDaySumOut(year,month,26)==0)
                &&(eachDaySumOut(year,month,27)==0)&&(eachDaySumOut(year,month,28)==0)
                &&(eachDaySumOut(year,month,29)==0)&&(eachDaySumOut(year,month,30)==0)
                &&(eachDaySumOut(year,month,31)==0))
        {
            axisYLine->setRange(0,10);
            for(int i=1;i<=31;i++)
            {
                series1->append(i,0);
            }
            for(int i=1;i<=31;i++)
            {
                series2->append(i,0);
            }
        }
        else
        {
            for(int i=1;i<=31;i++)
            {
                series1->append(i, eachDaySumIn(year,month,i));
            }
            for(int i=1;i<=31;i++)
            {
                series2->append(i, eachDaySumOut(year,month,i));
            }
        }
    }
    else if((month==4)||(month==6)||(month==9)||(month==11))
    {
        axisXLine->setRange(0,30);
        axisXLine->setTickCount(6);
        if((eachDaySumIn(year,month,1)==0)&&(eachDaySumIn(year,month,2)==0)
                &&(eachDaySumIn(year,month,3)==0)&&(eachDaySumIn(year,month,4)==0)
                &&(eachDaySumIn(year,month,5)==0)&&(eachDaySumIn(year,month,6)==0)
                &&(eachDaySumIn(year,month,7)==0)&&(eachDaySumIn(year,month,8)==0)
                &&(eachDaySumIn(year,month,9)==0)&&(eachDaySumIn(year,month,10)==0)
                &&(eachDaySumIn(year,month,11)==0)&&(eachDaySumIn(year,month,12)==0)
                &&(eachDaySumIn(year,month,13)==0)&&(eachDaySumIn(year,month,14)==0)
                &&(eachDaySumIn(year,month,15)==0)&&(eachDaySumIn(year,month,16)==0)
                &&(eachDaySumIn(year,month,17)==0)&&(eachDaySumIn(year,month,18)==0)
                &&(eachDaySumIn(year,month,19)==0)&&(eachDaySumIn(year,month,20)==0)
                &&(eachDaySumIn(year,month,21)==0)&&(eachDaySumIn(year,month,22)==0)
                &&(eachDaySumIn(year,month,23)==0)&&(eachDaySumIn(year,month,24)==0)
                &&(eachDaySumIn(year,month,25)==0)&&(eachDaySumIn(year,month,26)==0)
                &&(eachDaySumIn(year,month,27)==0)&&(eachDaySumIn(year,month,28)==0)
                &&(eachDaySumIn(year,month,29)==0)&&(eachDaySumIn(year,month,30)==0)
                &&(eachDaySumOut(year,month,1)==0)&&(eachDaySumIn(year,month,2)==0)
                &&(eachDaySumOut(year,month,3)==0)&&(eachDaySumOut(year,month,4)==0)
                &&(eachDaySumOut(year,month,5)==0)&&(eachDaySumOut(year,month,6)==0)
                &&(eachDaySumOut(year,month,7)==0)&&(eachDaySumOut(year,month,8)==0)
                &&(eachDaySumOut(year,month,9)==0)&&(eachDaySumOut(year,month,10)==0)
                &&(eachDaySumOut(year,month,11)==0)&&(eachDaySumOut(year,month,12)==0)
                &&(eachDaySumOut(year,month,13)==0)&&(eachDaySumOut(year,month,14)==0)
                &&(eachDaySumOut(year,month,15)==0)&&(eachDaySumOut(year,month,16)==0)
                &&(eachDaySumOut(year,month,17)==0)&&(eachDaySumOut(year,month,18)==0)
                &&(eachDaySumOut(year,month,19)==0)&&(eachDaySumOut(year,month,20)==0)
                &&(eachDaySumOut(year,month,21)==0)&&(eachDaySumOut(year,month,22)==0)
                &&(eachDaySumOut(year,month,23)==0)&&(eachDaySumOut(year,month,24)==0)
                &&(eachDaySumOut(year,month,25)==0)&&(eachDaySumOut(year,month,26)==0)
                &&(eachDaySumOut(year,month,27)==0)&&(eachDaySumOut(year,month,28)==0)
                &&(eachDaySumOut(year,month,29)==0)&&(eachDaySumOut(year,month,30)==0))
        {
            axisYLine->setRange(0,10);
            for(int i=1;i<=30;i++)
            {
                series1->append(i,0);
            }
            for(int i=1;i<=30;i++)
            {
                series2->append(i,0);
            }
        }
        else
        {
            for(int i=1;i<=30;i++)
            {
                series1->append(i, eachDaySumIn(year,month,i));
            }
            for(int i=1;i<=30;i++)
            {
                series2->append(i, eachDaySumOut(year,month,i));
            }
        }
    }
    else
    {
        if(((year%4==0)&&(year%100!=0))||(year%400==0))
        {
            axisXLine->setRange(1,29);
            axisXLine->setTickCount(5);
            if((eachDaySumIn(year,month,1)==0)&&(eachDaySumIn(year,month,2)==0)
                    &&(eachDaySumIn(year,month,3)==0)&&(eachDaySumIn(year,month,4)==0)
                    &&(eachDaySumIn(year,month,5)==0)&&(eachDaySumIn(year,month,6)==0)
                    &&(eachDaySumIn(year,month,7)==0)&&(eachDaySumIn(year,month,8)==0)
                    &&(eachDaySumIn(year,month,9)==0)&&(eachDaySumIn(year,month,10)==0)
                    &&(eachDaySumIn(year,month,11)==0)&&(eachDaySumIn(year,month,12)==0)
                    &&(eachDaySumIn(year,month,13)==0)&&(eachDaySumIn(year,month,14)==0)
                    &&(eachDaySumIn(year,month,15)==0)&&(eachDaySumIn(year,month,16)==0)
                    &&(eachDaySumIn(year,month,17)==0)&&(eachDaySumIn(year,month,18)==0)
                    &&(eachDaySumIn(year,month,19)==0)&&(eachDaySumIn(year,month,20)==0)
                    &&(eachDaySumIn(year,month,21)==0)&&(eachDaySumIn(year,month,22)==0)
                    &&(eachDaySumIn(year,month,23)==0)&&(eachDaySumIn(year,month,24)==0)
                    &&(eachDaySumIn(year,month,25)==0)&&(eachDaySumIn(year,month,26)==0)
                    &&(eachDaySumIn(year,month,27)==0)&&(eachDaySumIn(year,month,28)==0)
                    &&(eachDaySumIn(year,month,29)==0)&&(eachDaySumOut(year,month,1)==0)
                    &&(eachDaySumIn(year,month,2)==0)
                    &&(eachDaySumOut(year,month,3)==0)&&(eachDaySumOut(year,month,4)==0)
                    &&(eachDaySumOut(year,month,5)==0)&&(eachDaySumOut(year,month,6)==0)
                    &&(eachDaySumOut(year,month,7)==0)&&(eachDaySumOut(year,month,8)==0)
                    &&(eachDaySumOut(year,month,9)==0)&&(eachDaySumOut(year,month,10)==0)
                    &&(eachDaySumOut(year,month,11)==0)&&(eachDaySumOut(year,month,12)==0)
                    &&(eachDaySumOut(year,month,13)==0)&&(eachDaySumOut(year,month,14)==0)
                    &&(eachDaySumOut(year,month,15)==0)&&(eachDaySumOut(year,month,16)==0)
                    &&(eachDaySumOut(year,month,17)==0)&&(eachDaySumOut(year,month,18)==0)
                    &&(eachDaySumOut(year,month,19)==0)&&(eachDaySumOut(year,month,20)==0)
                    &&(eachDaySumOut(year,month,21)==0)&&(eachDaySumOut(year,month,22)==0)
                    &&(eachDaySumOut(year,month,23)==0)&&(eachDaySumOut(year,month,24)==0)
                    &&(eachDaySumOut(year,month,25)==0)&&(eachDaySumOut(year,month,26)==0)
                    &&(eachDaySumOut(year,month,27)==0)&&(eachDaySumOut(year,month,28)==0)
                    &&(eachDaySumOut(year,month,29)==0))
            {
                axisYLine->setRange(0,10);
                for(int i=1;i<=29;i++)
                {
                    series1->append(i, 0);
                }
                for(int i=1;i<=29;i++)
                {
                    series2->append(i, 0);
                }
            }
            else
            {
                for(int i=1;i<=29;i++)
                {
                    series1->append(i, eachDaySumIn(year,month,i));
                }
                for(int i=1;i<=29;i++)
                {
                    series2->append(i, eachDaySumOut(year,month,i));
                }
            }
        }
        else
        {
            axisXLine->setRange(1,28);
            axisXLine->setTickCount(10);
            if((eachDaySumIn(year,month,1)==0)&&(eachDaySumIn(year,month,2)==0)
                    &&(eachDaySumIn(year,month,3)==0)&&(eachDaySumIn(year,month,4)==0)
                    &&(eachDaySumIn(year,month,5)==0)&&(eachDaySumIn(year,month,6)==0)
                    &&(eachDaySumIn(year,month,7)==0)&&(eachDaySumIn(year,month,8)==0)
                    &&(eachDaySumIn(year,month,9)==0)&&(eachDaySumIn(year,month,10)==0)
                    &&(eachDaySumIn(year,month,11)==0)&&(eachDaySumIn(year,month,12)==0)
                    &&(eachDaySumIn(year,month,13)==0)&&(eachDaySumIn(year,month,14)==0)
                    &&(eachDaySumIn(year,month,15)==0)&&(eachDaySumIn(year,month,16)==0)
                    &&(eachDaySumIn(year,month,17)==0)&&(eachDaySumIn(year,month,18)==0)
                    &&(eachDaySumIn(year,month,19)==0)&&(eachDaySumIn(year,month,20)==0)
                    &&(eachDaySumIn(year,month,21)==0)&&(eachDaySumIn(year,month,22)==0)
                    &&(eachDaySumIn(year,month,23)==0)&&(eachDaySumIn(year,month,24)==0)
                    &&(eachDaySumIn(year,month,25)==0)&&(eachDaySumIn(year,month,26)==0)
                    &&(eachDaySumIn(year,month,27)==0)&&(eachDaySumIn(year,month,28)==0)
                    &&(eachDaySumOut(year,month,1)==0)&&(eachDaySumIn(year,month,2)==0)
                    &&(eachDaySumOut(year,month,3)==0)&&(eachDaySumOut(year,month,4)==0)
                    &&(eachDaySumOut(year,month,5)==0)&&(eachDaySumOut(year,month,6)==0)
                    &&(eachDaySumOut(year,month,7)==0)&&(eachDaySumOut(year,month,8)==0)
                    &&(eachDaySumOut(year,month,9)==0)&&(eachDaySumOut(year,month,10)==0)
                    &&(eachDaySumOut(year,month,11)==0)&&(eachDaySumOut(year,month,12)==0)
                    &&(eachDaySumOut(year,month,13)==0)&&(eachDaySumOut(year,month,14)==0)
                    &&(eachDaySumOut(year,month,15)==0)&&(eachDaySumOut(year,month,16)==0)
                    &&(eachDaySumOut(year,month,17)==0)&&(eachDaySumOut(year,month,18)==0)
                    &&(eachDaySumOut(year,month,19)==0)&&(eachDaySumOut(year,month,20)==0)
                    &&(eachDaySumOut(year,month,21)==0)&&(eachDaySumOut(year,month,22)==0)
                    &&(eachDaySumOut(year,month,23)==0)&&(eachDaySumOut(year,month,24)==0)
                    &&(eachDaySumOut(year,month,25)==0)&&(eachDaySumOut(year,month,26)==0)
                    &&(eachDaySumOut(year,month,27)==0)&&(eachDaySumOut(year,month,28)==0))
            {
                axisYLine->setRange(0,10);
                for(int i=1;i<=28;i++)
                {
                    series1->append(i, 0);
                }
                for(int i=1;i<=28;i++)
                {
                    series2->append(i, 0);
                }
            }
            else
            {
                for(int i=1;i<=28;i++)
                {
                    series1->append(i, eachDaySumIn(year,month,i));
                }
                for(int i=1;i<=28;i++)
                {
                    series2->append(i, eachDaySumOut(year,month,i));
                }
            }
        }
    }

    QChart *m_chartLine = new QChart();
    if(ui->theme_btn->isChecked())
        m_chartLine->setTheme(QChart::ChartThemeBlueCerulean);//设置深色主题
    else
        m_chartLine->setTheme(QChart::ChartThemeLight);//设置系统主题
    m_chartLine->setAnimationOptions(QChart::AllAnimations);//设置启用或禁用动画
    //chart->setBackgroundBrush(QBrush(QColor(170,170,255)));//设置背景色,主题和背景二选一
    m_chartLine->setDropShadowEnabled(true);//是否背景阴影
    m_chartLine->setLocalizeNumbers(true);//数字是否本地化
    m_chartLine->legend()->show();//legend是否显示，show和hide
    m_chartLine->addSeries(series1);//添加系列到QChart上
    m_chartLine->addSeries(series2);//添加系列到QChart上
    m_chartLine->createDefaultAxes();//创建默认轴

    m_chartLine->setAxisX(axisXLine,series1);
    m_chartLine->setAxisX(axisXLine,series2);
    m_chartLine->setAxisY(axisYLine,series1);
    m_chartLine->setAxisY(axisYLine,series2);

    m_chartLine->setTitle("折线图");//设置标题

    m_chartLine->legend()->setAlignment(Qt::AlignRight);//右边对齐
    m_chartLine->legend()->setBackgroundVisible(true);//设置背景是否可视
    m_chartLine->legend()->setVisible(true);//设置是否可视
    QChartView *chartViewLine = new QChartView(m_chartLine);
    chartViewLine->setRenderHint(QPainter::Antialiasing);
    //折线图窗口
    QGridLayout *baseLayoutLine = new QGridLayout();
    baseLayoutLine->addWidget(chartViewLine, 1, 0);
    setLayout(baseLayoutLine);
    QGridLayout *vlayoutLine = new QGridLayout;
    vlayoutLine->addLayout(baseLayoutLine, 0, 0);
    QWidget *widgetLine = new QWidget;
    widgetLine->setLayout(vlayoutLine);
    //折线图添加到stackedWidget
    ui->chartView->addWidget(widgetLine);
    return widgetLine;
}

//按键音效
void MainWindow::musicPlay()
{
    if(!ui->music_btn->isChecked()) //按下是关  不按下是开
    {
        QMediaPlayer *btnSound = new QMediaPlayer;
        btnSound->setMedia(QUrl("qrc:/music/buttonMusic.mp3"));
        btnSound->setVolume(30);
        btnSound->play();
    }
}

//主界面展示账单
void MainWindow::mainCashBook()
{
    int i=0;
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString cur_year = current_date_time.toString("yyyy");
    QString cur_month = current_date_time.toString("MM");
    QString cur_day = current_date_time.toString("dd");
    QSqlQuery query;
    QStandardItemModel * mode = new QStandardItemModel(ui->showTableView);
    QHeaderView* headerView = ui->showTableView->horizontalHeader();
    headerView->setSectionResizeMode(QHeaderView::Stretch);
    ui->showTableView->verticalHeader()->setVisible(false);
    mode->setColumnCount(8);
    mode->setHeaderData(0, Qt::Horizontal,"序号");
    mode->setHeaderData(1, Qt::Horizontal,"收入/支出");
    mode->setHeaderData(2, Qt::Horizontal,"金额");
    mode->setHeaderData(3, Qt::Horizontal,"主题");
    mode->setHeaderData(4, Qt::Horizontal,"时");
    mode->setHeaderData(5, Qt::Horizontal,"分");
    mode->setHeaderData(6, Qt::Horizontal,"心情");
    mode->setHeaderData(7, Qt::Horizontal,"备注");
    QString sql;
    sql = QString("select * from %1 where date_year = %2 and date_month = %3 and date_day = %4")
            .arg(username).arg(cur_year).arg(cur_month).arg(cur_day);
    query.exec(sql);
    while(query.next())
    {
        mode->setItem(i,0,new QStandardItem(QString("%1").arg(i+1)));
        mode->setItem(i,1,new QStandardItem(query.value(1).toString()));
        mode->setItem(i,2,new QStandardItem(query.value(2).toString()));
        mode->setItem(i,3,new QStandardItem(query.value(3).toString()));
        mode->setItem(i,4,new QStandardItem(query.value(7).toString()));
        mode->setItem(i,5,new QStandardItem(query.value(8).toString()));
        mode->setItem(i,6,new QStandardItem(query.value(9).toString()));
        mode->setItem(i,7,new QStandardItem(query.value(10).toString()));
        for(int j=0;j<=7;j++)
        {
            mode->item(i,j)->setTextAlignment(Qt::AlignCenter);
        }
        i++;
    }
    ui->showTableView->setModel(mode);
}

//查看界面展示账单
void MainWindow::showCashBook()
{
    int cur_year = ui->calendarWidget->selectedDate().year();
    int cur_month = ui->calendarWidget->selectedDate().month();
    int cur_day = ui->calendarWidget->selectedDate().day();
    int i=0;
    QSqlQuery query;
    QStandardItemModel * mode = new QStandardItemModel(ui->editTableView);
    QHeaderView* headerView = ui->editTableView->horizontalHeader();
    headerView->setSectionResizeMode(QHeaderView::Stretch);
    ui->editTableView->verticalHeader()->setVisible(false);
    mode->setColumnCount(9);
    mode->setHeaderData(0, Qt::Horizontal,"序号");
    mode->setHeaderData(1, Qt::Horizontal,"收入/支出");
    mode->setHeaderData(2, Qt::Horizontal,"金额");
    mode->setHeaderData(3, Qt::Horizontal,"主题");
    mode->setHeaderData(4, Qt::Horizontal,"年");
    mode->setHeaderData(5, Qt::Horizontal,"月");
    mode->setHeaderData(6, Qt::Horizontal,"日");
    mode->setHeaderData(7, Qt::Horizontal,"心情");
    mode->setHeaderData(8, Qt::Horizontal,"备注");
    QString sql;
    sql = QString("select * from %1 where date_year = %2 and date_month = %3 and date_day = %4")
            .arg(username).arg(cur_year).arg(cur_month).arg(cur_day);
    query.exec(sql);
    while(query.next())
    {
        mode->setItem(i,0,new QStandardItem(query.value(0).toString()));
        mode->setItem(i,1,new QStandardItem(query.value(1).toString()));
        mode->setItem(i,2,new QStandardItem(query.value(2).toString()));
        mode->setItem(i,3,new QStandardItem(query.value(3).toString()));
        mode->setItem(i,4,new QStandardItem(query.value(4).toString()));
        mode->setItem(i,5,new QStandardItem(query.value(5).toString()));
        mode->setItem(i,6,new QStandardItem(query.value(6).toString()));
        mode->setItem(i,7,new QStandardItem(query.value(9).toString()));
        mode->setItem(i,8,new QStandardItem(query.value(10).toString()));
        for(int j=0;j<=8;j++)
        {
            mode->item(i,j)->setTextAlignment(Qt::AlignCenter);
        }
        i++;
    }
    ui->editTableView->setModel(mode);
    ui->editTableView->setColumnHidden(0,true);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_check_btn_clicked()
{
    if(ui->stackedWidget->currentIndex()!=0)
    {
        ui->stackedWidget->setCurrentIndex(0);
        musicPlay();
        showCashBook();
    }
    else if(ui->stackedWidget->currentIndex()==0)
    {
        showCashBook();
    }
}

void MainWindow::on_main_btn_clicked()
{
    if(ui->stackedWidget->currentIndex()!=1)
    {
        ui->stackedWidget->setCurrentIndex(1);
        musicPlay();
        setLabel();
        mainCashBook();
    }
}

void MainWindow::on_chart_btn_clicked()
{
    if(ui->stackedWidget->currentIndex()!=2)
    {
        ui->stackedWidget->setCurrentIndex(2);
        musicPlay();
        chartShow();
    }
}

void MainWindow::on_limit_btn_clicked()
{
    if(ui->stackedWidget->currentIndex()!=3)
    {
        judgeBudget();
        ui->stackedWidget->setCurrentIndex(3);
    }
}

void MainWindow::on_info_btn_clicked()
{
    if(ui->stackedWidget->currentIndex()!=4)
    {
        ui->stackedWidget->setCurrentIndex(4);
        musicPlay();
    }
}

void MainWindow::on_add_btn_clicked()
{
    musicPlay();
    showAdd();
}

void MainWindow::radioButtonFun_4()
{
    if(ui->radioButton_4->isChecked())
    {
        ui->month->hide();
        ui->monthBox->hide();
        int year=ui->yearBox->currentText().toInt();
        ui->chartView->setCurrentWidget(chartPieWidget(year));
    }
    else
    {
        ui->month->show();
        ui->monthBox->show();
        int year=ui->yearBox->currentText().toInt();
        int month=(ui->monthBox->currentIndex())+1;
        ui->chartView->setCurrentWidget(chartPieWidget(year,month));
    }
}

void MainWindow::on_derive_btn_clicked()
{
    derive();
}

void MainWindow::on_music_btn_clicked()
{
    if(ui->music_btn->isChecked())
    {
        changeTheme();
    }
    else if(!ui->music_btn->isChecked())
    {
        musicPlay();
        changeTheme();
    }
}

void MainWindow::on_info_clicked()
{
    musicPlay();
    showInfo();
}

void MainWindow::on_update_btn_clicked()
{
    musicPlay();
    changePassword();
}

void MainWindow::on_theme_btn_clicked()
{
    musicPlay();
    changeTheme();
}

void MainWindow::on_delete_btn_clicked()
{
    QString sql;
    QSqlQuery query;
    sql = QString("delete from %1 where id = %2").arg(username).arg(id);
    query.exec(sql);
    showCashBook();
}

void MainWindow::on_clear_btn_clicked()
{
    int cur_year = ui->calendarWidget->selectedDate().year();
    int cur_month = ui->calendarWidget->selectedDate().month();
    int cur_day = ui->calendarWidget->selectedDate().day();
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->editTableView);
    if(ui->radioButton->isChecked())
    {
        QString sql;
        sql = QString("delete from %1 where date_year = %2 and date_month = %3 and date_day = %4")
                .arg(username).arg(cur_year).arg(cur_month).arg(cur_day);
        mode->setQuery(sql);
        showCashBook();
    }
    else if(ui->radioButton_2->isChecked())
    {
        QString sql;
        sql = QString("delete from %1 where date_year = %2 and date_month = %3")
                .arg(username).arg(cur_year).arg(cur_month);
        mode->setQuery(sql);
        showCashBook();
    }
    else if(ui->radioButton_3->isChecked())
    {
        QString sql;
        sql = QString("delete from %1 where date_year = %2")
                .arg(username).arg(cur_year);
        mode->setQuery(sql);
        showCashBook();
    }
}

void MainWindow::on_pie_btn_released()
{
    int yearChoose=ui->yearBox->currentText().toInt();
    int monthChoose=(ui->monthBox->currentIndex())+1;
    if(ui->radioButton_4->isChecked())
        ui->chartView->setCurrentWidget(chartPieWidget(yearChoose));
    else
        ui->chartView->setCurrentWidget(chartPieWidget(yearChoose,monthChoose));
    musicPlay();
}

void MainWindow::on_line_btn_released()
{
    int yearChoose=ui->yearBox->currentText().toInt();
    int monthChoose=(ui->monthBox->currentIndex())+1;
    if(ui->radioButton_4->isChecked())
        ui->chartView->setCurrentWidget(chartLineWidget(yearChoose));
    else
        ui->chartView->setCurrentWidget(chartLineWidget(yearChoose,monthChoose));
    musicPlay();
}

void MainWindow::on_bar_btn_released()
{
    int yearChoose=ui->yearBox->currentText().toInt();
    int monthChoose=(ui->monthBox->currentIndex())+1;
    if(ui->radioButton_4->isChecked())
        ui->chartView->setCurrentWidget(chartBarWidget(yearChoose));
    else
        ui->chartView->setCurrentWidget(chartBarWidget(yearChoose,monthChoose));
    musicPlay();
}

void MainWindow::on_calendarWidget_clicked(const QDate &date)
{
    showCashBook();
}

void MainWindow::on_editTableView_clicked(const QModelIndex &index)
{
    QAbstractItemModel *Imodel=ui->editTableView->model();
    int row=ui->editTableView->currentIndex().row();
    QModelIndex Iindex = Imodel->index(row,0);
    QVariant datatemp=Imodel->data(Iindex);
    id =datatemp.toInt(); //id即为所选择行的第一列的值——隐藏的id值
}

void MainWindow::on_dayBudgetSetButton_clicked()
{
    double day_limit,dayBudget;
    QString sql;
    QSqlQuery query1,query2;
    sql=QString("select * from users where name = '%1'").arg(username);
    query1.exec(sql);
    while(query1.next())
    {
        day_limit=query1.value(3).toDouble();
    }
    //接收新的预算
    dayBudget = QInputDialog::getDouble(this,"设置","每日预算：",day_limit);
    sql = QString("update users set day_limit = '%1' where name = '%2'").arg(dayBudget).arg(username);
    query2.exec(sql);
    qDebug()<<"更新成功";
    //更新预算进度标签
    QString daylabel = QString("每日预算：%1/%2").arg(ui->dayprogressBar->value()).arg(dayBudget);
    ui->dayBudgetSetlabel->setText(daylabel);
    ui->label_4->setText(QString("日限额:%1").arg(dayBudget));
    if (ui->dayprogressBar->value()>dayBudget)
    {
        ui->dayprogressBar->setMaximum(ui->dayprogressBar->value());
    }
    else ui->dayprogressBar->setMaximum(dayBudget);
    ui->dayBudgetSetlabel->show();
    judgeBudget();
}

void MainWindow::on_monthBudgetSetButton_clicked()
{
    double month_limit,monthBudget;
    QString sql;
    QSqlQuery query1,query2;
    sql=QString("select * from users where name = '%1'").arg(username);
    query1.exec(sql);
    while(query1.next())
    {
        month_limit=query1.value(4).toDouble();
    }
    //接收新的预算
    monthBudget = QInputDialog::getDouble(this,"设置","每月预算：",month_limit);
    sql = QString("update users set month_limit = '%1' where name = '%2'").arg(monthBudget).arg(username);
    query2.exec(sql);
    qDebug()<<"更新成功";
    //更新预算进度标签
    QString monthlabel = QString("每月预算：%1/%2").arg(ui->monthprogressBar->value()).arg(monthBudget);
    ui->monthBudgetSetlabel->setText(monthlabel);
    ui->label_5->setText(QString("月限额:%1").arg(monthBudget));
    if (ui->monthprogressBar->value()>monthBudget)
    {
        ui->monthprogressBar->setMaximum(ui->monthprogressBar->value());
    }
    else ui->monthprogressBar->setMaximum(monthBudget);
    ui->monthBudgetSetlabel->show();
    judgeBudget();
}

void MainWindow::on_yearBudgetSetButton_clicked()
{
    double year_limit,yearBudget;
    QString sql;
    QSqlQuery query1,query2;
    sql=QString("select * from users where name = '%1'").arg(username);
    query1.exec(sql);
    while(query1.next())
    {
        year_limit=query1.value(5).toDouble();
    }
    //接收新的预算
    yearBudget = QInputDialog::getDouble(this,"设置","每年预算：",year_limit);
    sql = QString("update users set year_limit = '%1' where name = '%2'").arg(yearBudget).arg(username);
    query2.exec(sql);
    qDebug()<<"更新成功";
    //更新预算进度标签
    QString yearlabel = QString("每年预算：%1/%2").arg(ui->yearprogressBar->value()).arg(yearBudget);
    ui->yearBudgetSetlabel->setText(yearlabel);
    ui->label_6->setText(QString("年限额:%1").arg(yearBudget));
    if (ui->yearprogressBar->value()>yearBudget)
    {
        ui->yearprogressBar->setMaximum(ui->yearprogressBar->value());
    }
    else ui->yearprogressBar->setMaximum(yearBudget);
    ui->yearBudgetSetlabel->show();
    judgeBudget();
}

void MainWindow::on_save_btn_clicked()
{
    updateDatabase();
}

void MainWindow::on_editTableView_doubleClicked(const QModelIndex &index)
{
    QAbstractItemModel *Imodel=ui->editTableView->model();
    int row=index.row();
    int column=index.column();
    QModelIndex model_1 = Imodel->index(row,column);
    QModelIndex model_2 = Imodel->index(row,1);
    QModelIndex model_3 = Imodel->index(row,4);
    QModelIndex model_4 = Imodel->index(row,5);
    QVariant datatemp_1=Imodel->data(model_1);
    QVariant datatemp_2=Imodel->data(model_2);
    QVariant datatemp_3=Imodel->data(model_3);
    QVariant datatemp_4=Imodel->data(model_4);
    if(column==1)
    {
        tableViewDelegate *cmb=new tableViewDelegate(this);
        cmb->type="收入/支出";
        ui->editTableView->setItemDelegateForColumn(1,cmb);
    }
    else if(column==2)
    {
        tableViewDelegate *cmb=new tableViewDelegate(this);
        cmb->type="金额";
        ui->editTableView->setItemDelegateForColumn(2,cmb);
    }
    else if(column==3)
    {
        if(datatemp_2.toString()=="收入")
        {
            tableViewDelegate *cmb=new tableViewDelegate(this);
            cmb->type="主题";
            cmb->in_out="收入";
            ui->editTableView->setItemDelegateForColumn(3,cmb);
        }
        else if(datatemp_2.toString()=="支出")
        {
            tableViewDelegate *cmb=new tableViewDelegate(this);
            cmb->type="主题";
            cmb->in_out="支出";
            ui->editTableView->setItemDelegateForColumn(3,cmb);
        }
    }
    else if(column==4)
    {
        tableViewDelegate *cmb=new tableViewDelegate(this);
        cmb->type="年";
        ui->editTableView->setItemDelegateForColumn(4,cmb);
    }
    else if(column==5)
    {
        tableViewDelegate *cmb=new tableViewDelegate(this);
        cmb->type="月";
        ui->editTableView->setItemDelegateForColumn(5,cmb);
    }
    else if(column==6)
    {
        tableViewDelegate *cmb=new tableViewDelegate(this);
        cmb->type="日";
        cmb->year=datatemp_3.toInt();
        cmb->month=datatemp_4.toInt();
        ui->editTableView->setItemDelegateForColumn(6,cmb);
    }
    else if(column==7)
    {
        tableViewDelegate *cmb=new tableViewDelegate(this);
        cmb->type="心情";
        ui->editTableView->setItemDelegateForColumn(7,cmb);
    }
}

void MainWindow::on_exit_btn_clicked()
{
    musicPlay();
    this->close();
    loginWindow *newloginWindow = new loginWindow();
    newloginWindow->show();
}

void MainWindow::on_pushButton_clicked()
{
    musicPlay();
    ui->stackedWidget->setCurrentIndex(1);
}
